package com.example.myprojects;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class NewsActivity extends AppCompatActivity {
   WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
    }

   void initView(){
        webView=findViewById(R.id.webview);
        Intent intent = getIntent();
        final String url=intent.getStringExtra("url");
       Log.e("initView: ",url );
        //加载网页，默认同构手机系统自带的浏览器去加载
       webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return  true;
            }
        });
        webView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        }else {
            super.onBackPressed();
        }
        }

    @Override
    protected void onPause() {
        super.onPause();
        //当页面失去焦点时，切换到后台不可见状态，通知内核停止所有动作
        webView.onPause();
    }
}
